package cz.muni.fi.pa036.kiss.daoimpl;

import cz.muni.fi.pa036.kiss.Entity.Kolej;
import cz.muni.fi.pa036.kiss.Entity.Student;
import cz.muni.fi.pa036.kiss.Entity.VybaveniStudenta;
import cz.muni.fi.pa036.kiss.dao.VybaveniStudentaDAO;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

public class VybaveniStudentaDAOImpl implements VybaveniStudentaDAO {

    @PersistenceContext
    private EntityManager em;

    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    @Override
    public void create(VybaveniStudenta vybaveni) {
        if (vybaveni == null) {
            throw new IllegalArgumentException("vybaveni is null");
        }
        if (vybaveni.getId() != null) {
            throw new IllegalArgumentException("vybaveni.id is not null");
        }

        em.persist(vybaveni);
    }

    @Override
    public VybaveniStudenta get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("null id");
        }
        VybaveniStudenta vybaveniStudenta = em.find(VybaveniStudenta.class, id);
        return vybaveniStudenta;
    }

    @Override
    public void update(VybaveniStudenta vybaveni) {
        if (vybaveni == null) {
            throw new IllegalArgumentException("vybaveni is null");
        }
        if (vybaveni.getId() == null) {
            throw new IllegalArgumentException("vybaveni.id is null");
        }

        em.merge(vybaveni);
    }

    @Override
    public void remove(VybaveniStudenta vybaveni) {
        if (vybaveni == null) {
            throw new IllegalArgumentException("vybaveni is null");
        }
        if (vybaveni.getId() == null) {
            throw new IllegalArgumentException("vybaveni.id is null");
        }
        VybaveniStudenta c = em.find(VybaveniStudenta.class, vybaveni.getId());
        em.remove(c);
    }

    @Override
    public List<VybaveniStudenta> findAll() {
        Query query = em.createQuery("SELECT c FROM VybaveniStudenta c");
        List<VybaveniStudenta> results = query.getResultList();

        return results;
    }

    @Override
    public List<VybaveniStudenta> findByKolej(Kolej kolej) {
        if (kolej == null) {
            throw new IllegalArgumentException("null kolej");
        }
        Long kolej_id = kolej.getId();
        Query query = em.createQuery("SELECT c FROM VybaveniStudenta c WHERE c.kolej.id = kolej_id");
        List<VybaveniStudenta> results = query.getResultList();

        return results;
    }

    @Override
    public List<VybaveniStudenta> findByStudent(Student student) {
        if (student == null) {
            throw new IllegalArgumentException("null student");
        }
        Long student_id = student.getId();
        Query query = em.createQuery("SELECT c FROM VybaveniStudenta c WHERE c.student.id = student_id");
        List<VybaveniStudenta> results = query.getResultList();

        return results;
    }
}
